package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.vo.Table;

/**
 * 订单子项数据操作类
 * 
 * @author peng
 * 
 */
public class TableUtil {

	public static Table getTableById(String id) {
		Cursor cursor = SQLUtil.query(SQLUtil.TABLE, null, "id=?",
				new String[] { id }, null, null, null);
		Table table = null;
		if (cursor.moveToFirst()) {
			do {				
				table = new Table();
				table.setId(cursor.getInt(0));
				table.setRoomID(cursor.getInt(1));
				table.setName(cursor.getString(2));
				table.setBrief(cursor.getString(3));
				table.setNumber(cursor.getInt(4));
				table.setStatus(cursor.getString(5));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return table;
	}

	public static ArrayList<Table> getAllTables() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery(
				"SELECT * FROM " + SQLUtil.TABLE, null);
		ArrayList<Table> tables = new ArrayList<Table>();
		if (cursor.moveToFirst()) {
			do {
				Table table = new Table();
				table.setId(cursor.getInt(0));
				table.setRoomID(cursor.getInt(1));
				table.setName(cursor.getString(2));
				table.setBrief(cursor.getString(3));
				table.setNumber(cursor.getInt(4));
				table.setStatus(cursor.getString(5));
				tables.add(table);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return tables;
	}

	public static long insertTable(Table table) {
		ContentValues values = new ContentValues();			
		values.put("id", table.getId());
		values.put("name", table.getName());
		values.put("roomID", table.getRoomID());
		values.put("brief", table.getBrief());
		values.put("number", table.getNumber());
		values.put("status", table.getStatus());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.TABLE, null,
				values);
	}

	public static long updateTable(Table table) {
		ContentValues values = new ContentValues();	
		values.put("id", table.getId());
		values.put("name", table.getName());
		values.put("roomID", table.getRoomID());
		values.put("brief", table.getBrief());
		values.put("number", table.getNumber());
		values.put("status", table.getStatus());
		return SQLUtil.getWritableDatabase().update(SQLUtil.TABLE, values,
				"id=?", new String[] { String.valueOf(table.getId()) });
	}

	public static void deleteTable(Table table) {
		SQLUtil.getWritableDatabase().delete(SQLUtil.TABLE, "id=?",
				new String[] { String.valueOf(table.getId()) });
	}
	

	public static void deleteAllTables() {
		SQLUtil.getWritableDatabase().delete(SQLUtil.TABLE,null,null);
	}
}
